import Vue from 'vue';
import Vuex from 'vuex';
import {
	http
} from '../common/api/index';

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		cartList: [], // 购物车数据
		userInfo: null, // 用户信息
		mainUserinfo: null,  // 主账号用户信息
		token: "",
		shareCode: null, // 邀请人邀请码
		themeColor: '#ff292c', // 主题色
		gestureAgain: true, // 是否需要二次验证
		appSearchHistory: [],
		
	},
	mutations: {
		init(state, item) {
			let _cartList = uni.getStorageSync('cartList');
			if (_cartList) {
				if (_cartList.length > 0) {
					let index = -1;
					_cartList.forEach((v, k) => {
						if (v.id == item.id) {
							index = k;
							v.count = item.count;
						}
					})
					if (index == -1) {
						_cartList.push(item);
					}
				} else if (_cartList.length == 0) {
					_cartList.push(item);
				}
			} else {
				_cartList = [];
				_cartList.push(item);
			}
			state.cartList = _cartList;
			uni.removeStorageSync('cartList');
			uni.setStorageSync('cartList', state.cartList);
		},
		delCart(state, arr) {
			let _cartList = uni.getStorageSync('cartList');
			if (_cartList) {
				if (_cartList.length > 0) {
					arr.forEach(id => {
						const index = _cartList.findIndex(item => item.id === id);
						if (index !== -1) {
							_cartList.splice(index, 1);
						}
					});
				}
				state.cartList = _cartList;
				uni.removeStorageSync('cartList');
				uni.setStorageSync('cartList', state.cartList);
			}
		},
		clearCart(state, arr) {
			state.cartList = arr;
			uni.removeStorageSync('cartList');
		},
		setBadge(state, num) {
			// #ifdef MP-WEIXIN
			let _index = 3;
			// #endif
			// #ifndef MP-WEIXIN
			let _index = 3;
			// #endif
			if (num) {
				if (Number(num) > 99) {
					uni.setTabBarBadge({
						index: _index,
						text: '99+'
					})
				} else {
					uni.setTabBarBadge({
						index: _index,
						text: num + ''
					})
				}
			} else {
				uni.removeTabBarBadge({
					index: _index
				})
			}
		},
		// 初始化APP,获取缓存的token,用户信息等其他缓存内容
		initApp(state) {
			const _userInfo = uni.getStorageSync('USER_INFO');
			if (_userInfo) {
				state.userInfo = _userInfo;
				global.userInfo = _userInfo;
			}
			const _mainUserinfo = uni.getStorageSync('MAIN_USER_INFO');
			if (_mainUserinfo) {
				state.mainUserinfo = _mainUserinfo;
				global.mainUserinfo = _mainUserinfo;
			}
			const _token = uni.getStorageSync('TOKEN');
			if (_token) {
				state.token = _token;
				global.token = _token;
			}
			const _dictionary = uni.getStorageSync('dictionary');
			if (_dictionary) {
				global.dictionary = _dictionary;
			}
			const _cartList = uni.getStorageSync('cartList');
			if (_cartList) {
				if (_cartList.length > 0) {
					state.cartList = _cartList;
				}
			}
		},
		// 添加搜索历史
		appSearchHistoryAdd(state, data) {
			if (!data.name.trim()) return
			let appSearchHistory = state.appSearchHistory.filter(item => item.name != data.name)
			appSearchHistory.unshift(data)
			state.appSearchHistory = appSearchHistory.slice(0, 10)
			uni.setStorageSync('appSearchHistory', state.appSearchHistory);
		},
		// 删除搜索历史
		appSearchHistoryDel(state, data) {
			state.appSearchHistory = state.appSearchHistory.filter(item => item.name != data.name)
			uni.setStorageSync('appSearchHistory', state.appSearchHistory);
		},
		// 清空搜索历史
		appSearchHistoryClean(state, data) {
			state.appSearchHistory = []
			uni.setStorageSync('appSearchHistory', state.appSearchHistory);
		},
		// 更新用户信息
		updateUserInfo(state, data) {
			if (state.userInfo) {
				state.userInfo = {
					...state.userInfo,
					...data,
				};
			} else {
				state.userInfo = data;
			}
			uni.setStorageSync('USER_INFO', state.userInfo);
			global.userInfo = state.userInfo;
			// if (state.userInfo.user_token) {
			// 	global.token = state.userInfo.user_token;
			// }
		},
		// 更新主账号用户信息
		updateMainUserInfo(state, data) {
			if (state.mainUserinfo) {
				state.mainUserinfo = {
					...state.userInfo,
					...data,
				};
			} else {
				state.mainUserinfo = data;
			}
			uni.setStorageSync('MAIN_USER_INFO', state.userInfo);
			global.mainUserinfo = state.userInfo;
		},
		updateToken(state, data) {
			state.token = data;
			global.token = state.token;
			uni.setStorageSync('TOKEN', state.token);
		},
		// 切换主题
		switchThemeColor(state, color) {
			state.themeColor = color;
		},
		// 退出登录
		logout(state) {
			state.userInfo = null;
			global.userInfo = null;
			state.mainUserinfo = null;
			global.mainUserinfo = null;
			state.token = "";
			global.token = "";
			state.cartList = [];
			state.verifyAgain = false; // 用户退出时二次验证为false，感觉没必要
			uni.removeStorageSync('USER_INFO');
			uni.removeStorageSync('TOKEN');
			uni.removeStorageSync('cartList');
			uni.removeStorageSync('subAccount');
		},
		// 二次验证
		setGestureAgain(state, data) {
			state.gestureAgain = data;
		}
	},
	// 从基本数据(state)派生的数据，相当于state的计算属性
	getters: {
		allNum(state) {
			let list = state.cartList;
			let total = 0;
			list.forEach((item) => {
				total += Number(item.num);
			});
			return total > 99 ? '99+' : total;
		},
		isLogin(state) {
			if (state.userInfo == null) {
				return false;
			} else {
				return true;
			}
		},
		// 是否会员
		isMember(state) {
			if (state.userInfo && state.userInfo.user_level) {
				return true;
			} else {
				return false;
			}
		},
		// 用户ID
		uid(state) {
			if (state.userInfo && state.userInfo.id) {
				return state.userInfo.id;
			} else {
				return 0;
			}
		},
		// 用户Token
		token(state) {
			if (state.userInfo && state.userInfo.user_token) {
				return state.userInfo.user_token;
			} else {
				return null;
			}
		},
		// 用户信息
		userInfo(state) {
			if (state.userInfo) {
				return state.userInfo;
			} else {
				return null;
			}
		},

	},
	// 和mutation的功能大致相同，不同之处在于 ==》1. Action 提交的是 mutation，而不是直接变更状态。 2. Action 可以包含任意异步操作。
	actions: {
		getCartList({
			dispatch,
			commit,
			getters,
			state
		}) {
			if (state.userInfo) {
				http.post(global.apiUrls.getCartList).then((res) => {
					if (res.data.code == 1) {
						let data = res.data.data;
						commit('init', data);
						commit('setBadge', getters.allNum);
					}
				});
			} else {
				console.log('getCartList2')
				commit('setBadge', 0)
			}
		}
	},
})
export default store;